import 'dart:async';

import 'package:wan_android_flutter/common/base/base_controller.dart';
import 'package:wan_android_flutter/common/network/model/login_model.dart';
import 'package:wan_android_flutter/common/utils/eventbus/event_bus_util.dart';
import 'package:wan_android_flutter/common/utils/mmkv/mmkv_util.dart';

import '../../../common/utils/eventbus/event_bus_type.dart';
import 'drawer_state.dart';

class DrawerLogic extends BaseController {
  final DrawerState state = DrawerState();

  StreamSubscription? _eventBus;

  @override
  void onInit() {
    super.onInit();
    _eventBus = eventBusUtil.listen((event) {
      if (event.type == EventBusType.refreshUserInfo) {
        LoginModel? result = event.result as LoginModel?;
        if (result != null) {
          state.userName.value = result.data!.username ?? "";
        }
      }
    });
    String userName = MMKVUtil.instance.getString("userName");
    if (userName.isNotEmpty) {
      state.userName.value = userName;
    }
  }

  @override
  void onClose() {
    _eventBus?.cancel();
    super.onClose();
  }
}
